<template>
    <div>
        <!-- 头部 -->
        <van-nav-bar title="精彩评论" left-arrow></van-nav-bar>
        <!-- 评论列表 -->
        <div class="item" v-for="item in commentList" :key="item.id">
            <div class="head">
                <img :src="item.user.head_img"
                    alt />
                <div>
                    <p>{{item.user.nickname}}</p>
                    <span>{{item.create_date}}</span>
                </div>
                <span @click="replyFn(item.id)">回复</span>
            </div>
            <!-- 嵌套评论内容star -->
            <commentItem @sendData="replyFn" v-if="item.parent" 
            :obj="item.parent"></commentItem>

            <!-- 嵌套评论内容end -->
            <div class="text">{{item.content}}</div>
        </div>
        <!-- 底部 -->
        <commentFooter ref="footer" @update="updateFn" :article="article"></commentFooter>
    </div>
</template>

<script>
import commentFooter from "@/components/commentFooter.vue";
import { getArticleDetail, post_comment } from "@/api/news.js"
import commentItem from "@/components/commentItem"
export default {
    components: { commentFooter,commentItem },
    data(){
        return {
            article: {},
            commentList: []
        }
    },
    created(){
        // 获取文章id
        // console.log(this.$route.query.id);
        // 获取文章详情
        getArticleDetail(this.$route.query.id).then(res=>{
            // console.log(38,res);
            this.article = res.data.data;
        })
        // 获取评论列表
        post_comment(this.$route.query.id).then(res=>{
            console.log(43,res);
            this.commentList = res.data.data;
        })
    },
    methods: {
        // 获取评论列表
        updateFn(){
            post_comment(this.$route.query.id).then(res=>{
                this.commentList = res.data.data;
                // 滚动到顶部
                window.scrollTo(0,0)
            })
        },
        // 回复评论
        replyFn(id){
            console.log(id);
            // 显示底部评论块的多行文本域
            // 可以先获取到底部评论块组件，
            // 然后再直接调用里面的focusFn函数即可实现
            this.$refs.footer.focusFn();
            this.$refs.footer.parent_id = id;
        }
    }
}
</script>

<style lang="less" scoped>
.van-nav-bar{
    border-bottom: 2px solid #ddd;
}
.item {
    padding: 10px;
    border-bottom: 1px solid #ccc;
    .head {
        display: flex;
        justify-content: space-between;
        align-items: center;
        > img {
            width: (50/360) * 100vw;
            height: (50/360) * 100vw;
            display: block;
            border-radius: 50%;
        }
        > div {
            flex: 1;
            display: flex;
            flex-direction: column;
            margin-left: 10px;
            > span {
                font-size: 12px;
                color: #999;
                line-height: 25px;
            }
        }
        > span {
            color: #999;
            font-size: 13px;
        }
    }
    .text {
        font-size: 14px;
        color: #333;
        padding: 20px 0 10px 0;
    }
}


</style>